package com.dehui.traders.viewAdapter;

import android.databinding.BindingAdapter;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.enums.MainTab;
import com.dehui.traders.ui.activity.MainActivity;

import me.goldze.mvvmhabit.binding.command.BindingCommand;

/**
 * Created by hqq on 18.3.30.
 */

public class TabHostViewAdapter {

    /**
     * 绑定监听
     *
     * @param fragmentTabHost
     * @param bindingCommand
     */
    @BindingAdapter(value = {"onTabChangeCommand"}, requireAll = false)
    public static void onChangeTab(final FragmentTabHost fragmentTabHost, final BindingCommand<String> bindingCommand) {
        fragmentTabHost.setOnTabChangedListener(tabId -> {
            bindingCommand.execute(tabId);
        });
    }


    @BindingAdapter(value = {"setTabData"})
    public static void setTabs(final FragmentTabHost mainTabhost, final MainTab[] mainTabs) {
        MainActivity context = MainActivity.context;
        mainTabhost.setup(context, context.getSupportFragmentManager(), R.id.main_container);
        for (MainTab tab : mainTabs) {
            TabHost.TabSpec tabSpec = mainTabhost.newTabSpec(context.getString(tab.resName));
            View itemView = LayoutInflater.from(context).inflate(R.layout.main_tab_item, null);
            ImageView imgItem = itemView.findViewById(R.id.iv_main_tab_item);
            TextView tvItem = itemView.findViewById(R.id.tv_main_tab_item);
            imgItem.setImageResource(tab.resIcon);
            tvItem.setText(context.getString(tab.resName));
            tabSpec.setIndicator(itemView);
//            tabSpec.setContent(tag -> new View(context));
            mainTabhost.addTab(tabSpec, tab.clz, null);
        }
        mainTabhost.setCurrentTab(0);
    }

}
